<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/8/18
 * Time: 11:15
 */

namespace app\logic\cron;


use app\common\lib\Common;
use app\common\lib\Guzzle;
use app\common\lib\GuzzleNew;
use app\common\lib\TencentCos;
use app\logic\BaseLogic;
use app\model\huitun\HuitunTalentDataModel;
use app\model\huitun\HuitunVideoDataModel;

class HuitunLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        parent::__construct();
    }

    public static function getInstance()
    {
//        if (is_object(self::$instance)) {
//            return self::$instance;
//        }

        return  new self();
    }

    public function getTalentData($keyword='推文',$from=1)
    {
        $micTime = round(microtime(true)*1000);
        $path = 'search/user?_t='.$micTime.'&from='.$from.'&sortField=follower_count_total&sortMod=desc&keyword='.$keyword;
        $headers = [
            'Cookie'=>'SESSION='.config('param.huitun_session_id','NmVhNTc0ZmUtZDg3Mi00MzIwLTlkZWYtMTBkZTQ4OGU0N2Vk'),
            'Content-Type'=>'application/x-www-form-urlencoded',
            'User-Agent'=>'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
        ];
        $data = (new GuzzleNew())->get('https://dyapi.huitun.com/',$path,$headers);
        if(!empty($data['data'])){
            //$avatarurlMap = HuitunTalentDataModel::getInstance()->pluck('avatarurl','authorid');
            foreach ($data['data'] as $val){
                $val = array_change_key_case($val,CASE_LOWER);
                $val['keyword'] = $keyword;
//                if(empty($avatarurlMap[$val['authorid']])){
//                    $res = TencentCos::getInstance()->uploadStringFile($val['avatarurl'],'huitun/talent');
//                    $val['avatarurl'] = 'https://'.$res['Location'];
//                }else{
//                    $val['avatarurl'] =$avatarurlMap[$val['authorid']];
//                }

                $this->autoCreateTableAndInsertData([
                   'table'=>'video_huitun_talent_data',
                   'keyid'=>'id',
                   'update_fields'=>['authorid','keyword'],
                   'attributes'=>$val
                ]);
            }

            sleep(1);
            $from++;
            return $this->getTalentData($keyword,$from);
        }elseif(!empty($data['code'])){
            $content = '灰豚同步数据异常: '."\n"."\n";
            $content .= '请求地址: '.'https://dyapi.huitun.com/'.$path."\n";
            $content .= '请求头: '.json_encode($headers,256)."\n";
            $content .= '异常信息: '.$data['message']??''."\n";

            Common::sendFeiShuNotice($content);
        }

        return 'success';
    }

    public function getVideoData($keyword='推文',$from=1)
    {
        $micTime = round(microtime(true)*1000);
        $hours = 720;
        $path = 'aweme/hotList?_t='.$micTime.'&from='.$from.'&keyword='.$keyword.'&hours='.$hours.'&sortField=digg_count&sortMod=desc&withFusionGoods=false&currentPolitics=true&imageTxt=false';
        $headers = [
            'Cookie'=>'SESSION='.config('param.huitun_session_id','NmVhNTc0ZmUtZDg3Mi00MzIwLTlkZWYtMTBkZTQ4OGU0N2Vk'),
            'Content-Type'=>'application/x-www-form-urlencoded',
            'User-Agent'=>'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
        ];
        $data = (new GuzzleNew())->get('https://dyapi.huitun.com/',$path,$headers);
        if(!empty($data['data'])){
            //$coveruriMap = HuitunVideoDataModel::getInstance()->pluck('coveruri','awemeid');
            foreach ($data['data'] as $val){
                $val = array_change_key_case($val,CASE_LOWER);
                $val['keyword'] = $keyword;
//                if(empty($coveruriMap[$val['awemeid']])){
//                    $res = TencentCos::getInstance()->uploadStringFile($val['coveruri'],'huitun/video');
//                    $val['coveruri'] = 'https://'.$res['Location'];
//                }else{
//                    $val['coveruri'] =$coveruriMap[$val['awemeid']];
//                }
                $this->autoCreateTableAndInsertData([
                   'table'=>'video_huitun_video_data',
                   'keyid'=>'id',
                   'update_fields'=>['awemeid','keyword'],
                   'attributes'=>$val
                ]);
            }

            sleep(1);
            $from++;
            return $this->getVideoData($keyword,$from);
        }elseif(!empty($data['code'])){
            $content = '灰豚同步数据异常: '."\n"."\n";
            $content .= '请求地址: '.'https://dyapi.huitun.com/'.$path."\n";
            $content .= '请求头: '.json_encode($headers,256)."\n";
            $content .= '异常信息: '.$data['message']??''."\n";

            Common::sendFeiShuNotice($content);
        }

        return 'success';
    }
}